<template>
	<div class="widgets bg-white">
		<el-input v-model.trim="searchText" placeholder="输入搜索内容" class="search__input" @keyup.enter.native="search"></el-input>
        <el-button type="primary" @click="search">查询</el-button>
        <div class="btn-group right">
	        <el-button type="info" @click="refresh">刷新</el-button>
	        <el-button type="primary" @click="add">新增</el-button>
	        <el-button type="danger" v-if="multipleSelection.length != 1 || multipleSelection[0]['name'] === '系统管理员' || multipleSelection[0]['name'] === '操作员组'" disabled>删除</el-button>
	        <el-button type="danger" v-else @click="del">删除</el-button>
	        <el-button type="danger" v-if="!isDisabled" disabled>禁用</el-button>
	        <el-button type="danger" v-else @click="disabedBtn(0, '禁用')">禁用</el-button>
	        <el-button type="success" v-if="!isDisabled" disabled>启用</el-button>
	        <el-button type="success" v-else @click="disabedBtn(1, '启用')">启用</el-button>
	        <el-button-group class="btn-other">
	            <el-button v-if="multipleSelection.length <= 0" disabled>
	                <img src="../../../assets/images/export.png" alt="">
	            </el-button>
	            <el-button title="导出" v-else @click="exportExcel">
	                <img src="../../../assets/images/export.png" alt="">
	            </el-button>                
	            <el-button title="打印">
	                <img src="../../../assets/images/print.png" alt="">
	            </el-button>
	            <el-button title="帮助">
	                <img src="../../../assets/images/help.png" alt="">
	            </el-button>
	        </el-button-group>
	    </div>
	</div>
</template>
<script>
export default {
	data() {
		return {
			searchText: '',
			isDisabled: false,
			multipleSelection: this.$store.state.userGroupStore.multipleSelection,
			selectRowId: this.$store.state.userGroupStore.selectRowId
		}
	},
	computed: {
		systemId() {
			return this.$store.state.systemId;
		}
	},
	watch: {
		searchText(newVal) {
			if(!newVal) {
                this.$store.dispatch('userGroupStore/getTableData');
            }
		},
		multipleSelection(newVal) {
			if(newVal.length > 0) {
				for(let i = 0; i < newVal.length; i++) {
					if(newVal[i]['name'] === '系统管理员' || newVal[i]['name'] === '操作员组') {
						this.isDisabled = false;
						return false;
					}
				}
				this.isDisabled = true;
			} else {
				this.isDisabled = false;
			}
		}
	},
	methods: {
		search() {			//搜索
			if(this.searchText) {
                let arr = this.$store.state.userGroupStore.tableData,
                    newArr = [];
                arr.forEach(item => {
                    if((item['toPinyin'] && item['toPinyin'].indexOf(this.searchText) != -1) || (item['name'] && item['name'].indexOf(this.searchText) != -1) || (item['toFirstChar'] && item['toFirstChar'].indexOf(this.searchText) != -1)) {
                        newArr.push(item);
                    }
                });
                this.$store.commit('userGroupStore/updateTableData', newArr);
            }
		},
		refresh() {				//刷新
			const loading = this.$loading({
                lock: true,
                text: '数据刷新中...',
                spinner: 'el-icon-loading',
                background: '#fff'
            });
            setTimeout(() => {
                loading.close();
            }, 1000);
		},
		add() {			//新增用户组
			this.$store.commit('userGroupStore/updateTableRowId');
			this.$util.openLayerIframe({title: '用户组-新增', zIndex: 13, width: '450px', height: '230px', url: this.$domain + '/#/basicData/userGroup/form'});
		},
		del() {
			this.$confirm('确定删除选中用户组？', '删除提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
            	this.$http.get('userGroup/deleteById?id=' + this.selectRowId[0] + '&systemId=' + this.systemId).then(res => {
            		if(res.data.code == 20001) {
            			this.$message({message: '删除成功', type: 'success', duration: 1500});
            			setTimeout(() => {
            				this.$store.dispatch('userGroupStore/getTreeDataAndGetTableData');		//刷新表格数据
            			}, 1000);
            		}
            	});
            }).catch(() => {
            	this.$message({message: '已取消删除', type: 'info', duration: 1500});
            });
		},
		disabedBtn(status, msg) {
			var type = {
                headers: {
                    'Content-type': 'application/json;charset=UTF-8'
                }
            }
            for(var i = 0; i < this.multipleSelection.length; i++) {
                if(this.multipleSelection[i]['locks'] == status) {
                    this.$alert(msg + '失败，其中包含已'+ msg +'选项，重新选择！', msg + '提示', {
                        confirmButtonText: '确定'
                    });
                    return false;
                }
            }
            this.$http.post('userGroup/enable?state=' + status, JSON.stringify(this.selectRowId), type).then(res => {         
                if(res.data.code == 20001) {
                    this.$message({message: msg + '成功', type: 'success', duration: 1500});
                    setTimeout(() => {  
                        this.$store.dispatch('userGroupStore/getTableData');		//刷新表格数据
                    }, 1000);
                } else {
                    this.$message.error(this.data.msg || msg + '失败');
                }
            });
		},
		exportExcel() {			//导出
			var idSting = this.selectRowId.join(',');           //将[1,2,3]格式转为 "1,2,3"
            this.$http.get('userGroup/export?systemId=' + this.systemId, {params: {list: idSting}}).then(res => {
                this.$message({message: '导出成功', type: 'success', duration: 1500});
                window.location.href = this.$url + 'userGroup/export?list=' + idSting + '&systemId=' + this.systemId;          //导出时触发
            }).catch(err => {
                this.$message.error(err || '导出失败');
            });
		}
	}
}
</script>